package ru.tomsk.autovokzal.tools.networking.command;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpConnection;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.message.BasicNameValuePair;

import ru.tomsk.autovokzal.tools.networking.HTTPClient;
import ru.tomsk.autovokzal.tools.networking.INetwokConnamd;
import android.content.Context;
import android.util.Log;

/**
 * Возвращает рейсы доступные для покупки билетов по направлению на указанную
 * дату необходимые параметры<br>
 * destinationPoint - пункт назначения<br>
 * date - дата отправления<br>
 * 
 */
public class TimeTableSyncCommand extends Command implements INetwokConnamd {

	public TimeTableSyncCommand(Map<String, String> parametrs) {
		super(parametrs);
		// TODO Auto-generated constructor stub
	}

	@Override
	public String getJSON(Context context) {

		try {
			HTTPClient getTimeTable = new HTTPClient(
					"http://avtovokzal.tomsk.ru/api/post.php");
			getTimeTable.setDefaultHeader();
			Map<String, String> timeTableParametrs = getParametrs();
			List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
			nameValuePairs.add(new BasicNameValuePair(apiUser,
					timeTableParametrs.get(apiUser)));
			nameValuePairs.add(new BasicNameValuePair(sign, timeTableParametrs
					.get(sign)));
			nameValuePairs.add(new BasicNameValuePair(method,
					timeTableParametrs.get(method)));
			nameValuePairs.add(new BasicNameValuePair(signAuth,
					timeTableParametrs.get(signAuth)));
			getTimeTable.setEntity(new UrlEncodedFormEntity(nameValuePairs));
			HttpResponse responce = getTimeTable.getResponce();
			
			int statusCode = responce.getStatusLine().getStatusCode();
			Log.i("ResponceTag", "Code:=" + statusCode);
			if (statusCode == 200) {
				String responseBody = getTimeTable.getResponseBody(responce
						.getEntity());

				Log.i("ResponceTag", responseBody);
			}
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	public static String stringToHex(String base) {
		StringBuffer buffer = new StringBuffer();
		int intValue;
		for (int x = 0; x < base.length(); x++) {
			int cursor = 0;
			intValue = base.charAt(x);
			String binaryChar = new String(Integer.toBinaryString(base
					.charAt(x)));
			for (int i = 0; i < binaryChar.length(); i++) {
				if (binaryChar.charAt(i) == '1') {
					cursor += 1;
				}
			}
			if ((cursor % 2) > 0) {
				intValue += 128;
			}
			buffer.append(Integer.toHexString(intValue) + " ");
		}
		return buffer.toString();
	}

}
